import com.sun.net.httpserver.HttpServer;
import handler.BettingOfferManagerHandler;
import model.StakeManager;
import model.CustomerSessions;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BettingOfferServer {

    private static final CustomerSessions customerSessions = new CustomerSessions();
    private static final StakeManager STAKE_MANAGER = new StakeManager();

    // 根据 CPU 核心数动态配置核心线程数，这里设置为 CPU 核心数 + 1
    private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() + 1;
    // 最大线程数设置为核心线程数的两倍
    private static final int MAX_POOL_SIZE = CORE_POOL_SIZE * 2;
    // 队列容量设置为100
    private static final int QUEUE_CAPACITY = 1000;

    private static final ExecutorService executor = new ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAX_POOL_SIZE,
            60L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_CAPACITY),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
        server.createContext("/", new BettingOfferManagerHandler(customerSessions, STAKE_MANAGER));
        server.setExecutor(executor);
        server.start();

        System.out.println("Server started on port 8001");
    }
}